/*
 * Created on Feb 16, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package ModuleBuilder.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import ModuleBuilder.*;
import javax.swing.event.*;
import java.beans.*;
import ModuleBuilder.model.*;

/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class GameParameter extends JPanel 
	implements ActionListener {
			
			
	public static final int _initGridSize=2;
	
	SpinnerModel	mapRowModel, mapColModel, numPixelModel, numUnitMeasureModel;
	BuilderManager manager;
	GameParameterModel gpModel=null;
	
	public GameParameter( BuilderManager m) {
		manager = m;
		this.setBorder(BorderFactory.createEmptyBorder(20,20,10,20));

		mapRowModel = new SpinnerNumberModel(_initGridSize, //initial value
									   1, //min
									   16, //max
									   1);                //step
		JSpinner mapRowSpinner = new JSpinner(mapRowModel);
		// mapRowSpinner.addChangeListener(this);

		mapColModel = new SpinnerNumberModel(_initGridSize, //initial value
									   1, //min
									   16, //max
									   1);                //step
		JSpinner mapColSpinner = new JSpinner(mapColModel);
		// mapColSpinner.addChangeListener(this);
		
//		numPixelModel = new SpinnerNumberModel(40,	// init value 
//												1,	// min
//												500, // max
//												1);		// step
//		JSpinner numPixelSpinner = new JSpinner(numPixelModel);
		// do layout stuff
		//
		JPanel mapSizePanel = new JPanel();
		// mapSizePanel.setLayout(new BoxLayout(mapSizePanel, BoxLayout.LINE_AXIS));
		JLabel mapSize1 = new JLabel("Map Size: ");
		JLabel mapSize2 = new JLabel(" tiles wide x ");
		JLabel mapsize3 = new JLabel(" tiles high");
		mapSizePanel.add(mapSize1);
		mapSizePanel.add(mapColSpinner);
		mapSizePanel.add(mapSize2);
		mapSizePanel.add(mapRowSpinner);
		mapSizePanel.add(mapsize3);
		
		// All future game parameter stuff all goes here

		// outer layout
		this.setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.add(Box.createRigidArea(new Dimension(0, 20)));
		mapSizePanel.setAlignmentX(JComponent.CENTER_ALIGNMENT);
		this.add( mapSizePanel );
		this.add( Box.createVerticalGlue());
		
		// The Save button
		JButton saveButton = new JButton("Save");
		saveButton.setAlignmentX(JComponent.CENTER_ALIGNMENT);
		this.add( saveButton );
		saveButton.addActionListener(this);
		
		// initialize the model into some set value
		getInputsIntoModel();
	}
	
	public void actionPerformed(ActionEvent e) {
		// by definition it's the "save" button being pushed
		getInputsIntoModel();
	}
	
	private void getInputsIntoModel(){
		// for each event fetch the content of the gameParameter into a Model
		GameParameterModel oldModel = gpModel;
		gpModel = new GameParameterModel();
		int numOfRow= ((Integer)(mapRowModel.getValue())).intValue();
		int numOfCol= ((Integer)(mapColModel.getValue())).intValue();
		gpModel.setMapNumOfRow(numOfRow);
		gpModel.setMapNumOfCol(numOfCol);
		if ( oldModel != null ) {		
			if ( (oldModel.getMapNumOfCol() != numOfCol )||
				(oldModel.getMapNumOfRow() != numOfRow)) {
					manager.mapSizeChanged();
				}
		}

	}
	
	private void loadModelIntoUI() {
		mapRowModel.setValue(new Integer(gpModel.getMapNumOfRow()));
		mapColModel.setValue(new Integer(gpModel.getMapNumOfCol()));
	}
	
	public int getMapGridRow() {
		return (gpModel.getMapNumOfRow());	
	}
	
	public int getMapGridCol() {
		return (gpModel.getMapNumOfCol());	
	}
	
	public GameParameterModel store() {
		// the size of the map model is already stored. But the actual map tiles needs to be 
		// stored as well.
		return (gpModel);
	}
	
	public void load(GameParameterModel mod) {
		gpModel = mod;
		loadModelIntoUI();
	}

}
